package com.lag.service;

import com.lag.spring.BeanPostProcessor;
import com.lag.spring.Component;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

@Component
public class LiaoguiBeanProcessor implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(String beanName, Object bean) {
        System.out.println("Before");
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(String beanName, Object bean) {
        System.out.println("After");
        if (beanName.equals("meberService")) {
            // 生成一个代理对象
            Object instance = Proxy.newProxyInstance(LiaoguiBeanProcessor.class.getClassLoader(), bean.getClass().getInterfaces(), new InvocationHandler() {
                @Override
                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                    System.out.println("切面逻辑");
                    return method.invoke(bean, args);
                }
            });
            return instance;
        }

        return bean;
    }
}
